<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/common/component/ymPrompt/skin/simple_gray/ymPrompt.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/common/component/ymPrompt/ymPrompt.js"></script>
<style type="text/css">
	
	.module {
		width: 150px;
		height: 20px;
		background-color: #EEEEEE;
		font-size: 12px;
		text-align: center;
		margin: 10px 0 0 25px;
		-webkit-border-radius: 15px;
		-moz-border-radius: 15px;
		border-radius: 15px;
		cursor: pointer;
		padding-top: 5px;
	}
	
	.module:hover {
		background-color: #CCCCCC;
		font-weight: bold;
	}
	
</style>
<div class="easyui-layout" fit="true">
	<div data-options="region:'west',split:true" title="数据字典分类" style="width:200px;overflow:hidden;">
		<div style="background:#fafafa;padding:5px;border:1px solid #ddd">
	        <a href="#" class="easyui-linkbutton" title="刷新" data-options="plain:true,iconCls:'icon-reload'" onclick="loadDictModule()"></a>
	        <a href="#" class="easyui-linkbutton" title="新增" data-options="plain:true,iconCls:'icon-add'" onclick="addDictModule()"></a>
	        <a href="#" class="easyui-linkbutton" title="修改" data-options="plain:true,iconCls:'icon-edit'" onclick="editDictModule()"></a>
	        <a href="#" class="easyui-linkbutton" title="删除" data-options="plain:true,iconCls:'icon-no'" onclick="deleteDictModule()"></a>
	    </div>
	    <div id="dictModuleList">
	    </div>
	</div>
	<div data-options="region:'center'" style="overflow:hidden;">
		<div id="gridToolbar" style="background:#fafafa;padding:5px;border:1px solid #ddd">
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="addDict()">新增</a>
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'" onclick="editDict()">修改</a>
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-no'" onclick="deleteDict()">删除</a>
	    </div>
	    <table id="dictGrid"></table>
		<div id="itemToolbar" style="background:#fafafa;padding:5px;border:1px solid #ddd">
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="loadDictItem()">刷新</a>
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="addDictItem()">新增</a>
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'" onclick="editDictItem()">修改</a>
	        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-no'" onclick="deleteDictItem()">删除</a>
	    </div>
	    <table id="dictGrid1"></table>
	</div>
</div>

<script type="text/javascript">

	var selectedModule;
	
	$(function(){
		$('#dictGrid').datagrid({
			width: $(document).width() - 482,
			height: ($("#rightComponent").height() - 69) / 2,
			nowrap: true,
			autoRowHeight: false,
			striped: true,
			collapsible:true,
			url:'dict/dict.action.jsp',
			queryParams: {
		        action: "getDictList",
		        dictionaryModuleId: "",
		        currentPage:"1",
		        pageSize:10
		    },
			loadMsg:'数据加载中请稍后……',
			sortName: 'createTime',
			sortOrder: 'desc',
			remoteSort: false,
			idField:'id',
			fitColumns: true,
			singleSelect: true,
			columns:[[
				{field:'id',title:'id',hidden:true},
				{field:'name',title:'名称',width:($(document).width() - 482) * 0.2,resizable:false},
				{field:'aliasName',title:'别名',width:($(document).width() - 482) * 0.2,resizable:false,sortable:true,
					sorter:function(a,b){
						return (a>b?1:-1);
					}
				},
				{field:'description',title:'描述',width:($(document).width() - 482) * 0.4,resizable:false},
				{field:'createTime',title:'创建时间',width:($(document).width() - 482) * 0.2,resizable:false}
			]],
			pagination:true,
			rownumbers:true,
			onClickRow: function(inx, data) {
				loadDictItem(data.id);
			}
		});
		var p = $('#dictGrid').datagrid('getPager');
		$(p).pagination({
			displayMsg: "当前显示 {from} - {to} ,共 {total} 条记录",
			onBeforeRefresh:function(){
				alert('before refresh');
			},
			onSelectPage: function(pageNumber, pageSize) {
		        $('#dictGrid').datagrid('reload',$.extend($('#dictGrid').datagrid('options').queryParams, {
		            currentPage: pageNumber,
		            pageSize: pageSize
	        	}));
		    }
		});
		$('#dictGrid1').datagrid({
			width:$(document).width() - 482,
			height:($("#rightComponent").height() - 138) / 2,
			nowrap: true,
			autoRowHeight: false,
			striped: true,
			collapsible:true,
			url:'dict/dict.action.jsp',
			queryParams: {
		        action: "getDictItemList",
		        dictId: ""
		    },
			sortName: 'value',
			sortOrder: 'desc',
			remoteSort: false,
			idField:'value',
			fitColumns: true,
			singleSelect: true,
			columns:[[
				{field:'text',title:'别名',width:($(document).width() - 482) * 0.5,resizable:false},
				{field:'value',title:'值',width:($(document).width() - 482) * 0.5,resizable:false,sortable:true,
					sorter:function(a,b){
						return (a>b?1:-1);
					}
				}
			]],
			pagination:false,
			rownumbers:true
		});
		loadDictModule();
	});

	
	function loadDictGrid(obj) {
		if(selectedModule == obj)
			return;
		// 清空grid
		$('#dictGrid').datagrid('loadData', { total: 0, rows: [] });
		if(obj != null) {
			$(selectedModule).css("background-color", "#EEEEEE");
			$(selectedModule).css("font-weight", "normal");
			selectedModule = obj;
			$(selectedModule).css("background-color", "#CCCCCC");
			$(selectedModule).css("font-weight", "bold");
		}
		var queryParams = $('#dictGrid').datagrid('options').queryParams;  
        queryParams.dictionaryModuleId = $(selectedModule).attr("id");
		$('#dictGrid').datagrid('reload');
	}
	
	function loadDictModule() {
		$.post("dict/dict.action.jsp", {action:"getDictModuleList"}, function(data){
			var dictModuleListHtml = "";
			var json = jQuery.parseJSON(data);
			$.each(json, function(inx, obj) {
				dictModuleListHtml += "<div id='" + obj.id + "' class='module' onclick='loadDictGrid(this)'>" + obj.name + "</div>";
			});
			$("#dictModuleList").html(dictModuleListHtml);
		});
	}
	
	function loadDictItem(dictId) {
		if(dictId == null) {
			dictId = $('#dictGrid').datagrid('getSelected').id;
		}
		// 清空grid
		$('#dictGrid1').datagrid('loadData', { total: 0, rows: [] });
		var queryParams = $('#dictGrid1').datagrid('options').queryParams;  
        queryParams.dictId = dictId;
		$('#dictGrid1').datagrid('reload');
	}
	
	function addDictModule() {
		ymPrompt.win({message:'dict/module_add.jsp',width:300,height:150,title:'数据字典分类新增',handler:null,iframe:true})
	}
	
	function editDictModule() {
		if(!selectedModule) {
			return;
		}
		ymPrompt.win({message:'dict/module_edit.jsp?id='+$(selectedModule).attr("id"),width:300,height:150,title:'数据字典分类修改',handler:null,iframe:true})
	}
	
	function deleteDictModule() {
		if(!selectedModule) {
			return;
		}
		ymPrompt.confirmInfo({message:'是否删除该数据字典分类？',handler:function(data){
			if(data == "ok") {
				$.post("dict/dict.action.jsp",{
					action:"deleteDictModule",
					id:$(selectedModule).attr("id")
				},function(data){
					if($.trim(data) == "success") {
						$(selectedModule).remove();
						// 清空grid
						$('#dictGrid').datagrid('loadData', { total: 0, rows: [] });
						$('#dictGrid1').datagrid('loadData', { total: 0, rows: [] });
					} else {
						alert("添加错误");
					}
				});
			}
		}});
	}
	
	function addDict() {
		if(!selectedModule) {
			alert("请选择数据字典分类");
			return;
		}
		ymPrompt.win({message:'dict/dict_add.jsp?dictionaryModuleId='+$(selectedModule).attr("id"),width:400,height:300,title:'数据字典新增',handler:null,iframe:true})
	}
	
	function editDict() {
		var selected = $('#dictGrid').datagrid('getSelected');
		if(!selected) {
			alert("请选择要修改的数据字典");
			return;
		}
		ymPrompt.win({message:'dict/dict_edit.jsp?id='+selected.id,width:400,height:300,title:'数据字典修改',handler:null,iframe:true});
	}
	
	function deleteDict() {
		var selected = $('#dictGrid').datagrid('getSelected');
		if(!selected) {
			alert("请选择要删除的数据字典");
			return;
		}
		ymPrompt.confirmInfo({message:'是否删除该数据字典？',handler:function(data){
			if(data == "ok") {
				$.post("dict/dict.action.jsp",{
					action:"deleteDict",
					dictId:selected.id
				},function(data){
					if($.trim(data) == "success") {
						$('#dictGrid').datagrid('deleteRow', $('#dictGrid').datagrid('getRowIndex', selected));
						// 清空值grid
						$('#dictGrid1').datagrid('loadData', { total: 0, rows: [] });
					} else {
						alert("删除错误");
					}
				});
			}
		}});
	}
	
	function addDictItem() {
		var selected = $('#dictGrid').datagrid('getSelected');
		if(!selected) {
			alert("请先选择数据字典");
			return;
		}
		ymPrompt.win({message:'dict/item_add.jsp?dictId='+selected.id,width:400,height:150,title:'数据字典值新增',handler:null,iframe:true});
	}
	
	function editDictItem() {
		var selected = $('#dictGrid1').datagrid('getSelected');
		if(!selected) {
			alert("请选择要修改的数据字典值");
			return;
		}
		var dictSelected = $('#dictGrid').datagrid('getSelected');
		ymPrompt.win({message:'dict/item_edit.jsp?dictId='+dictSelected.id+'&value='+selected.value+'&text='+encodeURI(escape(selected.text)),width:400,height:150,title:'数据字典值新增',handler:null,iframe:true});
	}
	
	function deleteDictItem() {
		var selected = $('#dictGrid1').datagrid('getSelected');
		if(!selected) {
			alert("请选择要删除的数据字典值");
			return;
		}
		var dictSelected = $('#dictGrid').datagrid('getSelected');
		ymPrompt.confirmInfo({message:'是否删除该数据字典值？',handler:function(data){
			if(data == "ok") {
				$.post("dict/dict.action.jsp",{
					action:"deleteDictItem",
					dictId:dictSelected.id,
					value:selected.value
				},function(data){
					if($.trim(data) == "success") {
						loadDictItem();
					} else {
						alert("删除错误");
					}
				});
			}
		}});
	}
	
</script>